window.innerCellsList = [],//场景数字格子的数组
    window.genreatingNumber = 0,//本次生成的数字格子的个数
    window.putDownNumber = 0;//可放下的个数

window.cellColor = [{ "num": 2, "color": "#ffffff" },
{ "num": 4, "color": "#12abf3" },
{ "num": 8, "color": "#6af33f" },
{ "num": 16, "color": "#f3fa3f" },
{ "num": 32, "color": "#fc8f9f" },
{ "num": 64, "color": "#66f71f" },
{ "num": 128, "color": "#81f5ff" },

],
    cc.Class({
        extends: cc.Component,

        properties: {

            cell: cc.Prefab,
            cellsNode: cc.Node,

            innercell: cc.Prefab,
            innercellInterface: cc.Node,

            qqq: cc.Node,
            aaa: cc.Node,



        },


        // LIFE-CYCLE CALLBACKS:

        // onLoad () {},

        start() {
            this.posId = [[0, 1, 2, 3],
            [4, 5, 6, 7, 8,],
            [9, 10, 11, 12, 13, 14],
            [15, 16, 17, 18, 19, 20, 21],
            [22, 23, 24, 25, 26, 27],
            [28, 29, 30, 31, 32],
            [33, 34, 35, 36]
            ];

            //正六边形的边长
            this.sideLength = 56;
            this.innerHeight = this.sideLength / 2 * 1.7;
            this.gap = 10;
            this.cells = [];
            this.generateInnrecell = [];
            this.initPos = { x: -500, y: 0 };
            this.creatCells();//生成棋盘

            //查找过的格子
            this.checkedCells = [];
            //准备合成的格子
            this.ready2CombineCells = [];



            this.screenH = cc.winSize.height;//屏幕的高
            this.screenW = cc.winSize.width;

            cc.director.getCollisionManager().enabled = true; //开启碰撞
            //cc.director.getCollisionManager().enabledDebugDraw = true;//显示碰撞区域
            // this.aaa.parent=this.cellsNode;//更换节点的父物体


            this.draggingType = [];//可拖动格子的种类

            this.generatePos = [];//可拖动格子的坐标
            this.draggingLatticePos();//设置可拖动格子的坐标

            this.draggingInnercell = [];//可拖动的格子的数组

            //下方6个格子的顺序  左上 右上 左 右 左下 右下
            this.below6cellPos = [{ "i": 1, "j": 0 }, { "i": 1, "j": 1 }, { "i": 0, "j": -1 }, { "i": 0, "j": 1 }, { "i": -1, "j": -1 }, { "i": -1, "j": 0 }];
            //上方6个格子的顺序  左上 右上 左 右 左下 右下
            this.upper6cellPos = [{ "i": 1, "j": -1 }, { "i": 1, "j": 0 }, { "i": 0, "j": -1 }, { "i": 0, "j": 1 }, { "i": -1, "j": 0 }, { "i": -1, "j": 1 }];


            this.addEventHandler();

            this.ergodicWholeCells();//bi
            this.generatecell();//生成格子
        },

        addEventHandler() {
            //按下事件监听
            this.node.on('touchstart', (event) => {
                this.startPoint = event.getLocation();
                this.startPoint.x -= this.screenW / 2;
                this.startPoint.y -= this.screenH / 2;

                // this.node.addChild(this.aaa);
                // console.log(this.startPoint);
                if (this.startPoint.x >= this.aaa.x - 150 && this.startPoint.x <= this.aaa.x + 150 &&
                    this.startPoint.y >= this.aaa.y - 150 && this.startPoint.y <= this.aaa.y + 150) {
                    this.IstoMove = true;
                    //console.log("按下");
                }
            });
            //移动事件监听
            this.node.on('touchmove', (event) => {
                this.touchMove(event);

            });
            //离开事件监听
            this.node.on('touchend', (event) => {
                // this.aaa.parent=this.cellsNode;//更换节点的父物体
                //  this.qqq.addChild(this.aaa);
                // this.aaa.x = this.qqq.x; this.aaa.y = this.qqq.y;
                this.IstoMove = false;

                //生成的格子都可以放下
                if (putDownNumber == genreatingNumber) {
                    for (let i = 0; i < this.generateInnrecell.length; i++) {
                        let _generate = this.generateInnrecell[i];
                        let _cell = _generate.getComponent('innercell');
                        // console.log("爱上降低" + _cell.collistionArrayI + "  " + _cell.collistionArrayJ);

                        let _num = _generate.getComponent('innercell').innerNumber;
                        this.cells[_cell.collistionArrayI][_cell.collistionArrayJ].getComponent('cell').subObject(_num);

                    }
                    for (let i = 0; i < this.generateInnrecell.length; i++) {
                        let _generate = this.generateInnrecell[i];
                        let _cell = _generate.getComponent('innercell');
                        // console.log("爱上降低" + _cell.collistionArrayI + "  " + _cell.collistionArrayJ);

                        let _num = _generate.getComponent('innercell').innerNumber;
                        //this.findTheSurroundingGrid(_cell);
                        this.ready2CombineCells=[];
                        this.checkedCells=[];
                        this.find(_cell);
                        //this.cells[_cell.collistionArrayI][_cell.collistionArrayJ].getComponent('cell').subObject(_num);
                        console.log('可以合成的格子'+i,"   合成数字 "+_num, this.ready2CombineCells);
                    }
                    // console.log('可以合成的格子', this.ready2CombineCells);
                    this.ergodicWholeCells();//遍历格子
                    this.generatecell();//生成可拖动的格子
                    // this.aaa.parent = this.innercellInterface;
                    // this.aaa.x = 0; this.aaa.y = 0;
                }
                this.aaa.x = this.qqq.x; this.aaa.y = this.qqq.y;
                putDownNumber = 0;//以碰撞的个数
            });
            //离开屏幕事件
            this.node.on('touchcancel', (event) => {
                this.aaa.x = this.qqq.x; this.aaa.y = this.qqq.y;
                this.IstoMove = false;
                putDownNumber = 0;//以碰撞的个数
            });
        },
        touchMove(event) {
            if (this.IstoMove == true) {
                //  console.log("移动");
                this.movePoint = event.getLocation();

                this.movePoint.x -= this.screenW / 2;
                this.movePoint.y -= this.screenH / 2;

                this.aaa.x = this.movePoint.x;
                this.aaa.y = this.movePoint.y;
            }
        },

        //根据id获取坐标位置
        getPositionById(id) {
            let pos = {};
            for (let i = 0; i < this.posId.length; i++) {
                let arr = this.posId[i];
                for (let j = 0; j < arr.length; j++) {
                    if (id == arr[j]) {
                        pos.x = Math.abs(3 - i) * (this.innerHeight + this.gap / 2) + this.initPos.x + j * (this.innerHeight * 2 + this.gap);
                        pos.y = this.initPos.y + (this.innerHeight + this.gap / 2) * 1.7 * (i - 3);
                        return pos;
                    }
                }
            }
        },

        //创建棋盘
        creatCells() {
            innerCellsList = [];
            for (let i = 0; i < this.posId.length; i++) {
                let arr = this.posId[i];
                let _innercelllist = [];
                let _cell = [];
                for (let j = 0; j < arr.length; j++) {

                    let pos = {};
                    pos.x = Math.abs(3 - i) * (this.innerHeight + this.gap / 2) + this.initPos.x + j * (this.innerHeight * 2 + this.gap);
                    pos.y = this.initPos.y + (this.innerHeight + this.gap / 2) * 1.7 * (i - 3);

                    let cell = cc.instantiate(this.cell);
                    cell.setPosition(pos.x, pos.y);
                    cell.getComponent('cell').id = arr[j];
                    cell.getComponent('cell').arrayI = i;
                    cell.getComponent('cell').arrayJ = j;

                    this.cellsNode.addChild(cell);
                    _cell.push(cell);
                    _innercelllist.push(null);
                }
                this.cells.push(_cell);
                innerCellsList.push(_innercelllist);
            }

            // let cellNum = 37;
            // for (let i = 0; i < cellNum; i++) {
            //     let cell = cc.instantiate(this.cell);
            //     let pos = this.getPositionById(i);
            //     cell.setPosition(pos.x, pos.y);
            //     cell.getComponent('cell').id=i;
            //     this.cellsNode.addChild(cell);
            //     this.cells.push(cell);
            // }

        },

        //可以放吗
        isPutable() {
            let bool = true;





            return bool;
        },
        //遍历整个棋盘
        ergodicWholeCells() {
            this.draggingType = [];

            for (let i = 0; i < this.cells.length; i++) {
                let _cell = this.cells[i];
                for (let j = 0; j < _cell.length; j++) {
                    // console.log(this.cells[i][j].getComponent('cell').IsSurePut);
                    //格子可以放东西 是空的
                    if (this.cells[i][j].getComponent('cell').IsSurePut == true) {
                        this.cellType(0);

                        if (i < Math.floor(this.cells.length / 2)) {
                            if (this.cells[i + 1] != null) {
                                if (this.cells[i + 1][j] != null) {
                                    if (this.cells[i + 1][j].getComponent('cell').IsSurePut == true) {
                                        //左上角是空的
                                        this.cellType(1);
                                    }
                                }
                                if (this.cells[i + 1][j + 1] != null) {
                                    if (this.cells[i + 1][j + 1].getComponent('cell').IsSurePut == true) {
                                        //右上角是空的
                                        this.cellType(2);
                                    }
                                }
                            }
                        } else {
                            if (this.cells[i + 1] != null) {
                                if (this.cells[i + 1][j - 1] != null) {
                                    if (this.cells[i + 1][j - 1].getComponent('cell').IsSurePut == true) {
                                        //左上角是空的
                                        this.cellType(1);
                                    }
                                }
                                if (this.cells[i + 1][j] != null) {
                                    if (this.cells[i + 1][j].getComponent('cell').IsSurePut == true) {
                                        //右上角是空的
                                        this.cellType(2);
                                    }
                                }

                            }
                        }
                        if (this.cells[i][j + 1] != null) {
                            if (this.cells[i][j + 1].getComponent('cell').IsSurePut == true) {
                                //右边是空的
                                this.cellType(3);
                            }
                        }


                    }


                    //////////////////////

                }
            }
            //console.log(this.draggingType);
            ///////////////////////
        },
        //格子状态   单个 single   左上Left upper   右上 Right upper  右侧  Right
        cellType(_type) {
            for (let i = 0; i < this.draggingType.length; i++) {
                if (_type == this.draggingType[i]) {
                    return;
                }
            }
            this.draggingType.push(_type);
        },
        //可拖动格子坐标
        draggingLatticePos() {
            let pos0 = {};
            pos0.x = 0;
            pos0.y = 0;
            //左上
            let pos1 = {};
            pos1.x = -1 * (this.innerHeight + this.gap / 2) + 0 * (this.innerHeight * 2 + this.gap);
            pos1.y = (this.innerHeight + this.gap / 2) * 1.7 * 1;
            //右上
            let pos2 = {};
            pos2.x = 1 * (this.innerHeight + this.gap / 2) + 0 * (this.innerHeight * 2 + this.gap);
            pos2.y = (this.innerHeight + this.gap / 2) * 1.7 * 1;

            //右
            let pos3 = {};
            pos3.x = 0 * (this.innerHeight + this.gap / 2) + 1 * (this.innerHeight * 2 + this.gap);
            pos3.y = (this.innerHeight + this.gap / 2) * 1.7 * 0;

            let pos = [pos1, pos2, pos3];

            let _type0 = [];
            _type0.push(pos0)
            this.generatePos.push(_type0);
            for (let i = 0; i < pos.length; i++) {
                let _pos1 = {};
                _pos1.x = 0 - pos[i].x / 2;
                _pos1.y = 0 - pos[i].y / 2;

                let _pos2 = {};
                _pos2.x = pos[i].x - pos[i].x / 2;
                _pos2.y = pos[i].y - pos[i].y / 2;

                let _type1 = [];
                _type1.push(_pos1, _pos2);
                this.generatePos.push(_type1);
            }


            // this.generatePos = [pos0,pos1, pos2, pos3];//可拖动的格子坐标的数组
        },

        //生成
        generatecell() {
            if (this.draggingType == null) return;
            // this.wallInterface.removeAllChildren();//清空子物体
            this.generateInnrecell = [];
            this.aaa.removeAllChildren();

            let _ty = this.draggingType[Math.floor(Math.random() * this.draggingType.length)];//场景上能出现的位置情况

            let _POS = this.generatePos[_ty];//相应的坐标数据
            for (let i = 0; i < _POS.length; i++) {
                let _innercell = cc.instantiate(this.innercell);

                //let zz = Math.ceil(Math.random() * 10);
                let zz = cellColor[Math.floor(Math.random() * cellColor.length)].num;
                _innercell.getComponent('innercell').innerNumber = zz;

                _innercell.setPosition(_POS[i].x, _POS[i].y);
                this.aaa.addChild(_innercell);
                this.generateInnrecell.push(_innercell);
            }



            // let xx = Math.floor(Math.random() * this.generatePos.length);
            // let _pos = this.generatePos[xx];
            // let _innercell2 = cc.instantiate(this.innercell);
            // let zz2 = Math.ceil(Math.random() * 10);
            // _innercell2.getComponent('innercell').innerNumber = zz2;
            // _innercell2.getComponent('innercell').zxzx = 2;
            // _innercell2.setPosition(_pos.x, _pos.y);
            // this.aaa.addChild(_innercell2);
            // this.generateInnrecell.push(_innercell2);

            putDownNumber = 0;//以碰撞的个数
            genreatingNumber = this.generateInnrecell.length;//需碰撞的个数

        },

        //找周围的六个格子
        findTheSurroundingGrid(cell) {
            let _i = cell.collistionArrayI;
            let _j = cell.collistionArrayJ;
            let _number = cell.innerNumber;
            let array = [];
            let _6direction;
            if (_i < this.cells.length / 2) {
                _6direction = this.below6cellPos;
            } else {
                _6direction = this.upper6cellPos;
            }

            for (let i = 0; i < _6direction.length; i++) {

                if (this.cells[_i + _6direction[i].i] != null) {
                    if (this.cells[_i + _6direction[i].i][_j + _6direction[i].j] != null) {
                        let _c = this.cells[_i + _6direction[i].i][_j + _6direction[i].j];
                        if (_c.getComponent('cell').number == _number) {
                            // this.cells[_i + _6direction[i].i][_j + _6direction[i].j].getComponent('cell').hideObject();

                            array.push(_c.getComponent('cell'));
                            //    let ll=this.cells[_i + _6direction[i].i][_j + _6direction[i].j].getComponent('cell').id;
                            //    checkedCells.push(ll);
                            //    this.findTheSurroundingGrid(_i + _6direction[i].i,_J + _6direction[i].j,_number);
                        }
                    }

                }

            }

            return array;

        },

        isReady(_cell) {
            for (let i = 0; i < this.ready2CombineCells.length; i++) {
                const cell = this.ready2CombineCells[i];
                if (_cell.id == cell.id) {
                    return true;
                }
            }
            return false;
        },

        isChecked(_cell) {
            for (let i = 0; i < this.checkedCells.length; i++) {
                const cell = this.checkedCells[i];
                if (cell.id == _cell.id) {
                    return true;
                }
            }
            return false;
        },


        find(cell) {
            this.checkedCells.push(cell);
            let array = this.findTheSurroundingGrid(cell);
            for (let i = 0; i < array.length; i++) {
                let _cell = array[i];
                if (!this.isReady(_cell)) {
                    this.ready2CombineCells.push(_cell);
                }
                if (!this.isChecked(_cell)) {
                    this.find(_cell);
                }
            }
        }


        // update (dt) {},
    });
